import logging
import os
from datetime import datetime

_is_initialized = False

def setup_logging():
    global _is_initialized
    if not _is_initialized:
        # 创建logs目录（如果不存在）
        logs_dir = 'logs'
        os.makedirs(logs_dir, exist_ok=True)
        
        # 生成日志文件名（使用当前日期）
        log_file = os.path.join(logs_dir, f'{datetime.now().strftime("%Y-%m-%d")}.log')
        
        # 配置根logger
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            handlers=[
                logging.FileHandler(log_file, encoding='utf-8'),  # 写入到文件处理器
                logging.StreamHandler()  # 现实中控制台处理器
            ]
        )
        _is_initialized = True

def get_logger(name):
    setup_logging()  # 确保日志系统已初始化
    return logging.getLogger(name)
